//
//  HDApplyIndemnityThirdCell.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/7/19.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit
// import HXPhotoPicker

enum ApplyIndemnityType: Int {
    case Menzhen = 1, Zhuyuan
}

class HDApplyIndemnityThirdCell: UITableViewCell {
    var model:HDApplyIndemnityThirdCellModel!{
        didSet{
            
            
            switch model.row {
            case 0:
                typeLabel.text = "收据"
                typeDescribeLabel.text = "请上传电子收据（或照片）"
                break
            case 1:
                if kind == .Menzhen{
                    typeLabel.text = "费用清单"
                    typeDescribeLabel.text = "请上传费用清单"
                }else{
                    typeLabel.text = "住院结算单"
                    typeDescribeLabel.text = "请上传住院结算单"
                }
               
                break
            case 2:
                if kind == .Menzhen{
                    typeLabel.text = "门诊处方"
                    typeDescribeLabel.text = "请上传门诊处方"
                }else{
                    typeLabel.text = "住院清单"
                    typeDescribeLabel.text = "请上传住院清单"
                }
                
                break
            case 3:
                if kind == .Menzhen{
                    typeLabel.text = "病历资料"
                    typeDescribeLabel.text = "请上传病历资料"
                }else{
                    typeLabel.text = "出院小结/诊断证明"
                    typeDescribeLabel.text = "请上传出院小结/诊断证明"
                }
                
                break
            case 4:
                typeLabel.text = "手工审批表或分割"
                typeDescribeLabel.text = "涉及社保结转后或第三方报销"
                break
            case 5:
                typeLabel.text = "如子女需要关系证明"
                typeDescribeLabel.text = "请上传户口簿或出生证明"
                break
            default:
                typeLabel.text = "收据"
                typeDescribeLabel.text = "请上传电子收据（或照片）"
                break
            }
        }
    }
    
    var photoViewChangeHeightBlock:((_ cell:HDApplyIndemnityThirdCell)->Void)?
    
    let manager = HXPhotoManager(type: HXPhotoManagerSelectedTypePhoto)
    let toolManager = HXDatePhotoToolManager.init()
    var photoView:HXPhotoView?
    var kind: ApplyIndemnityType?  // 门诊 住院

    @IBOutlet weak var redView: UIView!
    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var typeDescribeLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
    self.kind = .Menzhen
    self.redView.layer.cornerRadius = 3
    self.redView.layer.masksToBounds = true
        setupUI()

    }
    
    fileprivate func setupUI() {
        self.selectionStyle = .none
        photoView = HXPhotoView(frame: CGRect(x: 10, y: 90, width: 300, height: 0), manager: manager)
        photoView?.manager.configuration.photoMaxNum = 1000
        photoView?.manager.configuration.navBarBackgroudColor = UIColor.white
        photoView?.delegate = self
        photoView?.hideDeleteButton = true
        photoView?.previewShowDeleteButton = false
        photoView?.manager.configuration.photoCanEdit = false
        self.contentView.addSubview(photoView!)
        
        self.model = HDApplyIndemnityThirdCellModel()
        /// 单元格默认的高度
        model?.cellHeight = self.bounds.height
        model?.photoViewHeight = self.bounds.height
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    @IBAction func exampleAction(_ sender: UIButton) {
        HDDisplayImgView.initHDDisplayImgView(type: model.row!)
    }
    
    
}
extension HDApplyIndemnityThirdCell:HXPhotoViewDelegate{
    func photoView(_ photoView: HXPhotoView!, updateFrame frame: CGRect) {
        if frame.size.height == ((self.model?.photoViewHeight)!-20) {
            return
        }
        self.model?.photoViewHeight = frame.size.height + 20
        if (self.photoViewChangeHeightBlock != nil) {
            self.photoViewChangeHeightBlock!(self)
        }
    }
    
    func photoView(_ photoView: HXPhotoView!, changeComplete allList: [HXPhotoModel]!, photos: [HXPhotoModel]!, videos: [HXPhotoModel]!, original isOriginal: Bool) {
        var localPhotos = [HXPhotoModel]()
        for photo in photos {
            if photo.networkPhotoUrl == nil {
                localPhotos.append(photo)
            }
        }
       
        
        toolManager.getSelectedImageList(localPhotos, success: {[unowned self] (list:[UIImage]?) in
            
            if let list = list {
                self.model?.photos = list
            }
            let count = self.model.photos?.count
            if count != nil{
                self.countLabel.text = String(format: "已传%d张",count!)
            }
        }) {
        }
        //
        //        toolManager.getSelectedImageList(photos, success: {[unowned self] (list:[UIImage]?) in
        //            self.model?.photos = list ?? nil
        //        }) {
        //        }
    }
    
}

class HDApplyIndemnityThirdCellModel: NSObject {
    var cellHeight:CGFloat = 0
    var photoViewHeight:CGFloat = 120
    var photos:[UIImage]?
    var row: NSInteger?
    
}

